//
//  QRCodeViewController.swift
//  SwiftWB
//
//  Created by 吴志勇 on 2019/7/1.
//  Copyright © 2019 wzy. All rights reserved.
//

import UIKit
import AVFoundation

class QRCodeViewController: UIViewController {
    //冲击波的约束
   
 
    @IBOutlet weak var scanLineView: UIImageView!
    

    @IBOutlet weak var containerHeightCons: NSLayoutConstraint!
    @IBOutlet weak var scanLineCons: NSLayoutConstraint!
    @IBOutlet weak var customTabbar: UITabBar!
    override func viewDidLoad() {
        super.viewDidLoad()
        //1.设置默认选中
        customTabbar.selectedItem = customTabbar.items?.first
        
        //2.添加监听，监听底部工具条点击
        customTabbar.delegate = self
        
        //3.开始扫描二维码
        scanQRCode()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
       
        startAnimation()
    }
    
    //MARK: -内部控制方法
    private func scanQRCode(){
        //1.判断输入能否添加到会话中
        if !session.canAddInput(input) {
            return
        }
        //2.判断输出能否添加到输出的会话中
        if !session.canAddOutput(output){
            return
        }
        //3.添加输入，输出到会话中
        session.addInput(input)
        session.canAddOutput(output)
        
        output.metadataObjectTypes = output.availableMetadataObjectTypes
        
        output.setMetadataObjectsDelegate(self , queue: DispatchQueue.main)
        
        session.startRunning()
    }
    
    //开启冲击波动画
    private func startAnimation()
    {
        //1.设置冲击波底部和容器顶部对齐
        scanLineCons.constant = -containerHeightCons.constant
        view.layoutIfNeeded()
        
        //2.执行扫描动画
        //在Swift中一般下不用谢self，也不推荐我们写self
        //一般情况下只有需要区分两个变量，或者在闭包中访问外界属性才需要加上self
        //优点可以提醒程序员主动思考当前self会不会好行程循环引用
        UIView .animate(withDuration: 2.0) {
            UIView.setAnimationRepeatCount(MAXFLOAT)
            self.scanLineCons.constant = self.containerHeightCons.constant
            self.view.layoutIfNeeded()
        }
    }
    
    @IBAction func photoBtnClick(_ sender: Any) {
    }
    
    @IBAction func closeBtnClick(_ sender: Any) {
        dismiss(animated: true, completion: nil)
    }
    
    //MARK: -懒加载
    //输入对象
    let device = AVCaptureDevice.default(for: .video)
    guard device != nil else{
        return
    }
    
    var input:AVCaptureDeviceInput
    do{
        input =
    }
    
    
        
    
    
        
    

    //画会话
    private lazy var session: AVCaptureSession = AVCaptureSession()
    
    //输出对象
    private lazy var output : AVCaptureMetadataOutput = AVCaptureMetadataOutput()
    
    
}

extension QRCodeViewController: UITabBarDelegate
{
    func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        //根据当前选中的按钮重新设置二维码容器高度
        containerHeightCons.constant = (item.tag == 1) ? 150 : 300
        view.layoutIfNeeded()
        
        scanLineView.layer.removeAllAnimations()
        
        startAnimation()
      
    }
}


extension QRCodeViewController : AVCaptureMetadataOutputObjectsDelegate{
    func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
        WZYLog(message: metadataObjects.last?.type.rawValue)
    }
}
